package top.yangyitao.lockManager.zookeeper;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * @author eetal
 */
public class CuratorFrameworkBuilder {
    //配置ip白名单
    private final static String connectionUlr = "localhost:2181";

    private static CuratorFramework instance;

    public static CuratorFramework getInstance() {
        if(instance == null){
            synchronized (CuratorFrameworkBuilder.class){
                if(instance == null){
                    instance = CuratorFrameworkFactory.builder()
                            .connectString(connectionUlr).sessionTimeoutMs(5000).connectionTimeoutMs(5000)
                            .retryPolicy(new ExponentialBackoffRetry(1000, 3))
                            .build();
                    init();
                }
            }
        }
        return instance;
    }
    public static void init(){
        instance.start();
    }
    public static void shutdown(){
        if(instance != null){
            instance.close();
            instance = null;
        }
    }
}
